<!--
  ~  Licensed to the Apache Software Foundation (ASF) under one or more
  ~  contributor license agreements.  See the NOTICE file distributed with
  ~  this work for additional information regarding copyright ownership.
  ~  The ASF licenses this file to You under the Apache License, Version 2.0
  ~  (the "License"); you may not use this file except in compliance with
  ~  the License.  You may obtain a copy of the License at
  ~
  ~     http://www.apache.org/licenses/LICENSE-2.0
  ~
  ~  Unless required by applicable law or agreed to in writing, software
  ~  distributed under the License is distributed on an "AS IS" BASIS,
  ~  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  ~  See the License for the specific language governing permissions and
  ~  limitations under the License.
  -->
<div class="flex flex-col h-full gap-y-2">
    <div class="flex-1">
        <ng-container>
            <div class="cluster-node-table h-full flex flex-col">
                <!-- allow filtering of the table -->
                <cluster-table-filter
                    [filteredCount]="filteredCount"
                    [totalCount]="totalCount"
                    [filterableColumns]="filterableColumns"
                    filterColumn="address"
                    (filterChanged)="applyFilter($event)"></cluster-table-filter>

                <div class="flex-1 relative">
                    <div class="listing-table overflow-y-auto absolute inset-0">
                        <table
                            mat-table
                            [dataSource]="dataSource"
                            matSort
                            matSortDisableClear
                            (matSortChange)="sortData($event)"
                            [matSortActive]="initialSortColumn"
                            [matSortDirection]="initialSortDirection">
                            <!-- Node Address -->
                            <ng-container matColumnDef="address">
                                <th mat-header-cell *matHeaderCellDef mat-sort-header title="Node Address">
                                    <div class="overflow-ellipsis overflow-hidden whitespace-nowrap">Node Address</div>
                                </th>
                                <td mat-cell *matCellDef="let item" [title]="formatNodeAddress(item)">
                                    {{ formatNodeAddress(item) }}
                                </td>
                            </ng-container>

                            <!-- Active Thread Count -->
                            <ng-container matColumnDef="activeThreadCount">
                                <th mat-header-cell *matHeaderCellDef mat-sort-header title="Active Thread Count">
                                    <div class="overflow-ellipsis overflow-hidden whitespace-nowrap">
                                        Active Threads
                                    </div>
                                </th>
                                <td mat-cell *matCellDef="let item" [title]="formatActiveThreadCount(item)">
                                    {{ formatActiveThreadCount(item) }}
                                </td>
                            </ng-container>

                            <!-- Queued column -->
                            <ng-container matColumnDef="queued">
                                <th
                                    mat-header-cell
                                    *matHeaderCellDef
                                    mat-sort-header
                                    title="Count / data size queued in the last 5 minutes">
                                    <div
                                        class="inline-block overflow-hidden overflow-ellipsis whitespace-nowrap space-x-1">
                                        <span
                                            [ngClass]="{
                                                underline:
                                                    multiSort.active === 'queued' && multiSort.sortValueIndex === 0
                                            }"
                                            >Queue</span
                                        >
                                        <span> / </span>
                                        <span
                                            [ngClass]="{
                                                underline:
                                                    multiSort.active === 'queued' && multiSort.sortValueIndex === 1
                                            }"
                                            >Size</span
                                        >
                                    </div>
                                </th>
                                <td mat-cell *matCellDef="let item" [title]="formatQueued(item)">
                                    {{ formatQueued(item) }}
                                </td>
                            </ng-container>

                            <!-- Status Column -->
                            <ng-container matColumnDef="status">
                                <th mat-header-cell *matHeaderCellDef mat-sort-header title="Status">
                                    <div class="overflow-ellipsis overflow-hidden whitespace-nowrap">Status</div>
                                </th>
                                <td mat-cell *matCellDef="let item" [title]="formatStatus(item)">
                                    {{ formatStatus(item) }}
                                </td>
                            </ng-container>

                            <!-- Started At Column -->
                            <ng-container matColumnDef="nodeStartTime">
                                <th mat-header-cell *matHeaderCellDef mat-sort-header title="Started At">
                                    <div class="overflow-ellipsis overflow-hidden whitespace-nowrap">Started At</div>
                                </th>
                                <td mat-cell *matCellDef="let item" [title]="formatStartTime(item)">
                                    @if (!item.nodeStartTime) {
                                        <div class="unset neutral-color">No value set</div>
                                    } @else {
                                        {{ formatStartTime(item) }}
                                    }
                                </td>
                            </ng-container>

                            <!-- Last Heartbeat Column -->
                            <ng-container matColumnDef="heartbeat">
                                <th mat-header-cell *matHeaderCellDef mat-sort-header title="Last Heartbeat">
                                    <div class="overflow-ellipsis overflow-hidden whitespace-nowrap">
                                        Last Heartbeat
                                    </div>
                                </th>
                                <td mat-cell *matCellDef="let item" [title]="formatHeartbeat(item)">
                                    @if (!item.heartbeat) {
                                        <div class="unset neutral-color">No value set</div>
                                    } @else {
                                        {{ formatHeartbeat(item) }}
                                    }
                                </td>
                            </ng-container>

                            <ng-container matColumnDef="actions">
                                <th mat-header-cell *matHeaderCellDef></th>
                                <td mat-cell *matCellDef="let item">
                                    <div class="flex items-center justify-end gap-x-2">
                                        @if (
                                            item.status === 'CONNECTED' ||
                                            item.status === 'CONNECTING' ||
                                            item.status === 'DISCONNECTED' ||
                                            item.status === 'OFFLOADED'
                                        ) {
                                            <button
                                                mat-icon-button
                                                type="button"
                                                [matMenuTriggerFor]="actionMenu"
                                                class="h-16 w-16 flex items-center justify-center icon global-menu">
                                                <i class="fa fa-ellipsis-v"></i>
                                            </button>
                                        }
                                        <mat-menu #actionMenu="matMenu" xPosition="before">
                                            <button mat-menu-item (click)="moreDetail(item)">
                                                <i class="fa fa-info-circle primary-color mr-2"></i>
                                                Detail
                                            </button>
                                            @if (item.status === 'CONNECTED' || item.status === 'CONNECTING') {
                                                <button mat-menu-item (click)="disconnect(item)">
                                                    <i class="fa fa-power-off primary-color mr-2"></i>
                                                    Disconnect
                                                </button>
                                            } @else if (item.status === 'DISCONNECTED') {
                                                <button mat-menu-item (click)="connect(item)">
                                                    <i class="fa fa-plug primary-color mr-2"></i>
                                                    Connect
                                                </button>
                                                <button mat-menu-item (click)="offload(item)">
                                                    <i class="fa fa-rotate-90 fa-upload primary-color mr-2"></i>
                                                    Offload
                                                </button>
                                                <button mat-menu-item (click)="remove(item)">
                                                    <i class="fa fa-trash primary-color mr-2"></i>
                                                    Delete
                                                </button>
                                            } @else if (item.status === 'OFFLOADED') {
                                                <button mat-menu-item (click)="connect(item)">
                                                    <i class="fa fa-plug primary-color mr-2"></i>
                                                    Connect
                                                </button>
                                                <button mat-menu-item (click)="remove(item)">
                                                    <i class="fa fa-trash primary-color mr-2"></i>
                                                    Delete
                                                </button>
                                            }
                                        </mat-menu>
                                    </div>
                                </td>
                            </ng-container>

                            <tr mat-header-row *matHeaderRowDef="displayedColumns; sticky: true"></tr>
                            <tr
                                mat-row
                                *matRowDef="let row; let even = even; columns: displayedColumns"
                                [class.even]="even"
                                (click)="select(row)"
                                [class.selected]="isSelected(row)"></tr>
                        </table>
                    </div>
                </div>
            </div>
        </ng-container>
    </div>
</div>
